package com.sunkey.log.audit.support;

import java.io.File;

import org.apache.log4j.ConsoleAppender;
import org.apache.log4j.DailyRollingFileAppender;
import org.apache.log4j.FileAppender;
import org.apache.log4j.Level;
import org.apache.log4j.Logger;
import org.apache.log4j.PatternLayout;
import org.apache.log4j.RollingFileAppender;
import org.springframework.beans.factory.InitializingBean;


/**
 * 
 * @author sunkey
 * @date Aug 25, 2014
 */
public class AuditLog4j implements com.sunkey.log.common.spi.Logger, InitializingBean{

    private String level = Level.INFO.toString();

    private String filePath = "/tmp/logs";

    private String appenderName;

    private String type = "rolling";

    /**
     * 统一track-开头
     */
    private String fileName = "track-fpx";

    private int maxBackups = 100;

    private String fileSize = "5MB";

    private String datePattern = "yyyy-MM-dd";

    private Logger logger;
    

    @Override
    public void info(String log) {
        logger.info(log);
    }
    
    @Override
    public void afterPropertiesSet() throws Exception {
        init();
    }
    
    private void init() {
        System.out.println("fpx log is initializing,level:" + level + ", fileName:" + fileName);
        logger = Logger.getLogger(AuditLog4j.class);
        logger.removeAllAppenders();
        logger.setAdditivity(false);
        logger.setLevel(Level.toLevel(level));

        PatternLayout layout = new PatternLayout();
        layout.setConversionPattern("%m%n");

        FileAppender fileAppender = null;

        if (type.equals("rolling")) {
            RollingFileAppender rollingFileAppender = new RollingFileAppender();
            rollingFileAppender.setMaxBackupIndex(maxBackups);
            rollingFileAppender.setMaxFileSize(fileSize);
            fileAppender = rollingFileAppender;
        } else if (type.equals("dailyRolling")) {
            DailyRollingFileAppender rollingFileAppender = new DailyRollingFileAppender();
            rollingFileAppender.setDatePattern(datePattern);
            fileAppender = rollingFileAppender;
        }

        if (fileAppender == null) {
            throw new IllegalArgumentException("the type attribute is incorrect");
        }

        fileAppender.setEncoding("UTF-8");
        // true:在已存在log文件后面追加 false:新log覆盖以前的log
        fileAppender.setAppend(true);

        fileAppender.setLayout(layout);
        String name = fileName + ".log";
        fileAppender.setFile(filePath + File.separatorChar + name);

        // 适用当前配置
        fileAppender.activateOptions();

        logger.addAppender(fileAppender);

        if (level.equalsIgnoreCase("DEBUG")) {
            ConsoleAppender consoleAppender = new ConsoleAppender();
            PatternLayout consoleLayout = new PatternLayout();
            consoleLayout.setConversionPattern("%d{yyyy-MM-dd HH:mm:ss} %p [%t] [%c] [%x] [%r] [%m]%n");
            consoleAppender.setLayout(consoleLayout);

            // 适用当前配置
            consoleAppender.activateOptions();

            logger.addAppender(consoleAppender);
        }

    }

    public void setLevel(String level) {
        this.level = level;
    }

    public void setFilePath(String filePath) {
        this.filePath = filePath;
    }

    public void setAppenderName(String appenderName) {
        this.appenderName = appenderName;
    }

    public void setType(String type) {
        this.type = type;
    }

    public void setFileName(String fileName) {
        this.fileName = fileName;
    }

    public void setMaxBackups(int maxBackups) {
        this.maxBackups = maxBackups;
    }

    public void setFileSize(String fileSize) {
        this.fileSize = fileSize;
    }

    public void setDatePattern(String datePattern) {
        this.datePattern = datePattern;
    }

    public String getLevel() {
        return level;
    }

    public String getFilePath() {
        return filePath;
    }

    public String getAppenderName() {
        return appenderName;
    }

    public String getType() {
        return type;
    }

    public String getFileName() {
        return fileName;
    }

    public int getMaxBackups() {
        return maxBackups;
    }

    public String getFileSize() {
        return fileSize;
    }

    public String getDatePattern() {
        return datePattern;
    }

}
